package com.example.shixun.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.shixun.dao.UserDao;
import com.example.shixun.pojo.Result;
import com.example.shixun.pojo.User;
import com.example.shixun.pojo.UserDTO;
import com.example.shixun.service.UserService;
import com.example.shixun.utils.JwtUtils;
import com.example.shixun.utils.Md5;
import com.example.shixun.utils.PageInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    Md5 md5=new Md5();
    @Resource
    private UserDao userDao;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public Result login(User user){
        System.out.println("UserService");
        user.setPassword(md5.code(user.getPassword()));
        User ruser =userDao.login(user);
        //System.out.println("ruser.id="+ruser.getId());
//        Integer id=ruser.getId();
//        System.out.println("Dao已执行,id="+id);
        if(ruser==null)  return Result.fail("登录失败，请检查账号密码");
        String jwtToken = JwtUtils.getJwtToken(ruser.getId().toString(), ruser.getName(),ruser.getRole());
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(ruser, userDTO);
        // 加入到redis中
        System.out.println(jwtToken);
        System.out.println(userDTO.toString());
        System.out.println(ruser.toString());
        stringRedisTemplate.opsForValue().set(jwtToken, JSON.toJSONString(userDTO), 1, TimeUnit.DAYS);
        return Result.success("登录成功",jwtToken);
    }

    @Override
    public Integer register(User user) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d=formatter.format(date);
        System.out.println("date:"+d);
        user.setPassword(md5.code(user.getPassword()));
        user.setRole("student");
        user.setCreateTime(d);
        user.setLastModifiedTime(d);
        user.setStatus(0);
        userDao.save(user);
        return null;
    }

    @Override
    public Result<Integer> updatePwd(User user) {
        user.setPassword(md5.code(user.getPassword()));
        int n = userDao.updatePwd(user);
        if(n>0) return  Result.success(n);
        return Result.fail("重置密码失败，请确认邮箱是否正确");
    }

    @Override
    public Result<Integer> findEmail(String email) {
        int n = userDao.findEmail(email);
        if(n>0) return Result.success("该邮箱已被注册",n);
        return Result.fail("该邮箱未被注册");
    }

    @Override
    public PageInfoUtil<User> getStudentList(PageInfoUtil<User> pageInfoUtil) {
        //该语句一定要在执行sql 语句之前，参数第一个参数代表当前第几页，第二个参数代表每页的数量
        PageHelper.startPage(pageInfoUtil.getPageNum(),pageInfoUtil.getPageSize());
        //System.out.println(pageInfoUtil.getPageSize());
        List<User> lists=this.userDao.findStudent(pageInfoUtil.getParams());
        PageInfo<User> pageInfo=  new PageInfo<User>(lists);
        System.out.println(pageInfo);
        pageInfoUtil.setPageNum(pageInfo.getPageNum());
        pageInfoUtil.setPageSize(pageInfo.getPageSize());
        pageInfoUtil.setPageTotal(pageInfo.getPages());
        pageInfoUtil.setTotal(pageInfo.getTotal());
        pageInfoUtil.setList(lists);
        return pageInfoUtil;
    }

    @Override
    public UserDTO getUserTokenInfo(String token) {
        //if(stringRedisTemplate==null) stringRedisTemplate=new StringRedisTemplate();
        String userJson=stringRedisTemplate.opsForValue().get(token);
        System.out.println("userJson="+userJson);
        return JSON.parseObject(userJson, UserDTO.class);
    }

    @Override
    public Result getUserById(Integer id) {
        if(id==null) return Result.fail("id==null");
        User user = userDao.findById(id);
        if(user==null) return Result.fail("未找到此id用户");
        return Result.success(user);
    }

    @Override
    public Result updateUser(User user) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d=formatter.format(date);
        user.setLastModifiedTime(d);
        int n = userDao.updateUser(user);
        if(n<=0) return Result.fail("修改信息失败！");
        return Result.success("修改信息成功！");
    }

    @Override
    public Integer setStatus_0(Integer id) {
        return userDao.setStatus_0(id);
    }

    @Override
    public Integer setStatus_1(Integer id) {
        return userDao.setStatus_1(id);
    }

    @Override
    public Result setTeacher(Integer id) {
        Integer n=userDao.setTeacher(id);
        if(n>0) Result.success(n);
        return Result.fail("修改失败");
    }

    @Override
    public Result setStudent(Integer id) {
        Integer n=userDao.setStudent(id);
        if(n>0) Result.success(n);
        return Result.fail("修改失败");
    }
}
